package ie.deloitte.cvapp.webcontrollers;

import ie.deloitte.cvapp.domain.Skills;
import ie.deloitte.cvapp.form.SkillBeanForm;
import ie.deloitte.cvapp.service.CVService;
//import ie.deloitte.cvapp.service.SkillService;
//import ie.deloitte.cvapp.service.SkillService;
//import ie.deloitte.cvapp.service.SkillService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/skills/")
public class SkillsController {
	
	@Autowired
    private CVService cvService;
	
	
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *	This method is called when a request is made to display the skills.jsp page
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/skills", method = RequestMethod.GET)
	public String skills(Model model){
		
		model.addAttribute("skills",this.cvService.getAllSkills());
		
		return "skills/skills";
	}
	
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *	This method is called when a request is made to display the addSkill.jsp page
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/addSkill", method = RequestMethod.GET)
	public void setupForm(Model model){
		SkillBeanForm skillBean = new SkillBeanForm();
		model.addAttribute("skill", skillBean);		
	}
	
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *	This method is called when the submit button on addSkill.jsp is pressed
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/addSkill", method = RequestMethod.POST)
	public String submitForm(@ModelAttribute SkillBeanForm skillBean){

		System.out.println(skillBean);
		
		cvService.saveSkill(skillBean);
		
		return "redirect:skills";
	}

	
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * This method is called when a request is made to display the UpdateSkills.jsp page
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/UpdateSkills", method = RequestMethod.GET)
	public String update(@RequestParam String idSkill, Model model) {
		model.addAttribute("skills", cvService.findSkillById(idSkill));
		return "skills/UpdateSkills";
	}
	
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * This method is called when the update button on UpdateSkills.jsp is pressed
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/UpdateSkills", method = RequestMethod.POST)
	public String updateForm(@ModelAttribute("skills") Skills skills, BindingResult result) {
		//System.out.println("Employee updated");
		//System.out.println("Name " + employee.getName());
//		SkillBeanForm skillBeanForm = new SkillBeanForm();
		
//        skillBeanForm.setIdSkill(skills.getIdSkill());
//        skillBeanForm.setName(skills.getName());
       cvService.updateSkill(skills);
		
			return "redirect:skills";
		}
		
		
	}
	



